package cn.lds.healthj377.controller;

import cn.lds.healthj377.domain.HealthExamItem;
import cn.lds.healthj377.domain.HealthExamPackage;
import cn.lds.healthj377.domain.User;
import cn.lds.healthj377.dto.DsHealthConstant;
import cn.lds.healthj377.dto.ResponseDTO;
import cn.lds.healthj377.exception.SysException;
import cn.lds.healthj377.service.HealthExamItemService;
import cn.lds.healthj377.service.HealthExamPackageService;
import com.alibaba.druid.stat.TableStat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("exam_package")
public class ExamPackageController {

    private static final Logger log = LoggerFactory.getLogger(ExamPackageController.class);
    @Autowired
    private HealthExamPackageService healthExamPackageService;

    @Autowired
    private HealthExamItemService healthExamItemService;

//    @GetMapping
    @RequestMapping(value = "show_packages",method = RequestMethod.GET)
    public String showExamPackage(HealthExamPackage healthExamPackage, Model model) throws SysException {

        ResponseDTO<List<HealthExamPackage>> listResponseDTO = healthExamPackageService.selectPackage(healthExamPackage);

        model.addAttribute("result",listResponseDTO);

        return "show_health_package";

    }

    @RequestMapping(value = "packages_manage",method = RequestMethod.GET)
    public String examPackageManage(HealthExamPackage healthExamPackage, Model model) throws SysException {

        ResponseDTO<List<HealthExamPackage>> listResponseDTO = healthExamPackageService.selectPackage(healthExamPackage);

        model.addAttribute("result",listResponseDTO);

        return "health_package_manage";

    }

    @RequestMapping("/show_package_desc/{id}")
    public String showExamPackageBy(@PathVariable Integer id, Model model) throws SysException {

//        int i = 1/0;
        ResponseDTO<HealthExamPackage> healthExamPackageResponseDTO = healthExamPackageService.selectPackageById(id);
        model.addAttribute("result",healthExamPackageResponseDTO);
        return "show_package_desc";
    }

    @RequestMapping("add_package")
    public String addPackage(Model model){

        List<HealthExamItem> healthExamItems = healthExamItemService.selectHealthExamItemAll().getData();


        log.info("healthExamItems:{}",healthExamItems);
        model.addAttribute("healthExamItems",healthExamItems);

        return "add_package";
    }

    @RequestMapping("do_add_package")
    public String doAddPackage(HealthExamPackage healthExamPackage, Integer[] items, HttpSession session) throws SysException {

        User loginUser = (User) session.getAttribute("loginUser");
        healthExamPackage.setCreateBy(loginUser.getId());
        log.info("hep:{}",healthExamPackage);
        Arrays.stream(items).forEach(i -> log.info("项目编号：{}",i));

        healthExamPackageService.addPackage(healthExamPackage,items);
        return "redirect:packages_manage";
    }
}
